package com.heimo.agentlicence;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;

import com.heimo.agentlicence.task.DisciplineLoadTask;
import com.heimo.agentlicence.task.OnTaskCompleted;
import com.heimo.agentlicence.ui.DialogHelper;
import com.heimo.agentlicence.ui.adaptor.ScreenSlidePagerAdapter;
import com.heimo.agentlicence.ui.dialog.AboutDialog;
import com.heimo.agentlicence.ui.fragment.BasicFragment;

public class MainActivity extends FragmentActivity {
	private final String TAG = "MainActivity";
	private ViewPager mPager;
	private ScreenSlidePagerAdapter mPagerAdapter;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		initPager();
	}

	private void initPager() {
		mPager = (ViewPager) findViewById(R.id.pager);
		mPagerAdapter = new ScreenSlidePagerAdapter(this);
		mPager.setAdapter(mPagerAdapter);
		mPager.setOnPageChangeListener(new OnPageChangeListener() {

			@Override
			public void onPageScrollStateChanged(int arg0) {
				// TODO Auto-generated method stub
			}

			@Override
			public void onPageScrolled(int arg0, float arg1, int arg2) {
				// TODO Auto-generated method stub
			}

			@Override
			public void onPageSelected(int page) {
				BasicFragment fragment = mPagerAdapter.getItem(page);
				if (fragment.getCurrentLicence() == null) {
					mPager.setCurrentItem(0);
					return;
				}
				fragment.fnPageLoad();
			}
		});
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	@Override
	public boolean onMenuItemSelected(int menuId, MenuItem item) {
		Log.v(TAG, "onMenuItemSelected : " + item.getItemId() + " menuId : "
				+ menuId);
		switch (item.getItemId()) {
			case R.id.action_about:
				AboutDialog ab = new AboutDialog(this);
				ab.show();
				return true;
		}
		return false;
	}
	
	@Override
	public void onBackPressed() {
		if (mPager.getCurrentItem() == 0) {
			// If the user is currently looking at the first step, allow the
			// system to handle the
			// Back button. This calls finish() on this activity and pops the
			// back stack.
			super.onBackPressed();
		} else {
			// Otherwise, select the previous step.
			mPager.setCurrentItem(mPager.getCurrentItem() - 1);
		}
	}

	public void fnTest(View v) {
		Log.d(TAG, "fnTest");
		final MainActivity al = this;
		OnTaskCompleted completeAction = new OnTaskCompleted() {
			public void onTaskCompleted(Object s) {
			}

			public void onTaskFailed(String errMessage) {
				Log.d(TAG, errMessage);
				DialogHelper.getOkAlertDialog(al, "Error Fail", errMessage)
						.show();
			}
		};
		DisciplineLoadTask llt = new DisciplineLoadTask(al, completeAction);
		llt.fnLoadPage();

	}
}
